package jmini.service;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import jmini.dao.MiniDAO;
import jmini.dto.Pagination;
import jmini.iservice.IMiniService;
import jmini.modules.Module;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class MiniService implements IMiniService{
	
private Logger log = LoggerFactory.getLogger(MiniService.class);
	
	@Resource
	private MiniDAO miniDAO = null;

	@Override
	public String save(Module model){
		String id = null;
		try {
			id = miniDAO.save(model);
		} catch (IllegalArgumentException e) {
			log.error("save fail:",e);
		} catch (IllegalAccessException e) {
			log.error("save fail:",e);
		}
		return id;
	}

	@Override
	public void delete(Module model) {
		try {
			miniDAO.delete(model);
		} catch (IllegalArgumentException e) {
			log.error("delete fail:",e);
		} catch (IllegalAccessException e) {
			log.error("delete fail:",e);
		}
	}
	
	@Override
	public void update(Module model, String... fieldsNames) {
		try {
			List<String> fieldsList = null;
			if(fieldsNames != null && fieldsNames.length > 0){
				fieldsList = Arrays.asList(fieldsNames);
			}
			miniDAO.updateFields(model, fieldsList);
		} catch (IllegalArgumentException e) {
			log.error("update fail:",e);
		} catch (IllegalAccessException e) {
			log.error("update fail:",e);
		}
	}

	@Override
	public void update(Module model) {
		try {
			miniDAO.updateFields(model, null);
		} catch (IllegalArgumentException e) {
			log.error("update fail:",e);
		} catch (IllegalAccessException e) {
			log.error("update fail:",e);
		}
	}

	@Override
	public <T> T query(String id ,Class<T> requiredType){
		return miniDAO.query(id, requiredType);
	}

	@Override
	public void queryByPage(Pagination p, String whereStr, String orderStr, @SuppressWarnings("rawtypes") Class requiredType, Object... args) {
		miniDAO.queryByPage(p, whereStr, orderStr, requiredType, args);
	}

	@Override
	public void queryBySQLPage(Pagination p,
			String countSQLStr, String sqlStr, String whereStr,
			String orderStr, @SuppressWarnings("rawtypes") Class requiredType, Object... args) {
		miniDAO.queryBySQLPage(p, countSQLStr, sqlStr, whereStr, orderStr, requiredType, args);
	}

	@Override
	public <T> List<T> queryList(String whereStr, String orderStr,
			Class<T> requiredType, Object... args) {
		return miniDAO.queryList(whereStr, orderStr, requiredType, args);
	}

	@Override
	public <T> T queryOne(String whereStr, String orderStr,
			Class<T> requiredType, Object... args) {
		List<T> r = miniDAO.queryList(whereStr, orderStr, requiredType, args);
		if(r != null && r.size() > 0){
			return (T) r.get(0);
		}else{
			return null;
		}
	}

	@Override
	public Integer countData(String countSQLStr, Object... args) {
		return miniDAO.countData(countSQLStr, args);
	}

	@Override
	public Double sumData(String countSQLStr, Object... args) {
		return miniDAO.sumData(countSQLStr, args);
	}
	
	@Override
	public int updateBySQL(String sql, Object... args){
		return miniDAO.updateBySQL(sql, args);
	}

}
